﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace binary
{
    public static class XMLReader
    {
        public static ConfigSingle getConfig()
        {
            return XMLReader.getConfig("config.xml");
        }

        public static ConfigSingle getConfig(string file)
        {
            ConfigSingle c = new ConfigSingle();
            XmlDocument d = new XmlDocument();
            if (File.Exists(file))
            {
                d.Load(file);
            }
            XmlNodeList nodeList = d.SelectNodes("DBF2SQL/CONFIG");
            foreach (XmlNode node in nodeList)
            {
                if (node["USER"] != null)
                    c.user = node["USER"].InnerText;
                if (node["PASSWORD"] != null)
                    c.password = node["PASSWORD"].InnerText;
                if (node["HOST"] != null)
                    c.host = node["HOST"].InnerText;
                if (node["DBNAME"] != null)
                    c.dbName = node["DBNAME"].InnerText;
                if (node["LOG"] != null)
                    c.logTable = node["LOG"].InnerText;
                if (node["ERROR"] != null)
                    c.errorsTable = node["ERROR"].InnerText;
            }
            return c;
        }

        public static ConfigList getImportList(string file)
        {
            ConfigList import = new ConfigList();
            try
            {
                XmlDocument d = new XmlDocument();
                if (File.Exists(file))
                    d.Load(file);
                
                
                XmlNodeList nodeList = d.SelectNodes("DBF2SQL/CONFIG");
                foreach (XmlNode node in nodeList)
                {
                    
                    if(node["USER"]!= null)
                        import.user = node["USER"].InnerText;
                    if (node["PASSWORD"] != null)
                        import.password = node["PASSWORD"].InnerText;
                    if (node["HOST"] != null)
                        import.host = node["HOST"].InnerText;
                    if (node["DBNAME"] != null)
                        import.dbName = node["DBNAME"].InnerText;
                    if (node["DBFDESTINATION"] != null)
                        import.dbfDestination = node["DBFDESTINATION"].InnerText;
                    if (node["DBFSOURCE"] != null)
                        import.dbfSource = node["DBFSOURCE"].InnerText;
                    if (node["LOG"] != null)
                        import.logTable = node["LOG"].InnerText;
                    if (node["ERROR"] != null)
                     import.errorTable = node["ERROR"].InnerText;
                    if (node["FINISH"] != null)
                        import.finish = node["FINISH"].InnerText;
                    
                }


                nodeList = d.SelectNodes("DBF2SQL/TABLES/TABLE");
                foreach (XmlNode node in nodeList)
                {
                    string destination;
                    if(node.Attributes["DESTINATION"]!=null)
                         destination = node.Attributes["DESTINATION"].Value.ToString();
                    else
                        destination = node.InnerText;
                    import.tableList.Add(
                        new SingleTable{
                            name=node.InnerText
                            ,destinationName = destination
                        });
                }
            }
            catch
            {
                return null;
            }

            return import;
        }
    }
}
